package com.geeyuu.android;

import java.util.LinkedList;

import android.app.Activity;
import android.app.Application;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.geeyuu.android.databases.DataBaseHelper;
import com.geeyuu.android.utils.LogUtil;

public class GeeApplication extends Application {

    private static final String TAG = "GeeApplication";
    private LinkedList<Activity> activities;
    
    private GetLocationListener mGetLocationListener;
    private LocationClient mLocationClient = null;
    private BDLocationListener mLocListener = new MyLocationListenner();
    private static final int GET_LOCATION_INTERVAL = 60 * 1000 * 5;
    private static final int GET_LOCATION_TIME_OUT = 20 * 1000;
    
    public static interface GetLocationListener{
        public void onFinish(double[] lonAndLat);
        public void onFail(double[] lastLonAndLat);
    }
    
    public void setLocationListener(GetLocationListener getLocationListener){
        mGetLocationListener = getLocationListener;
        mLocationClient.requestLocation();
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        LogUtil.v(TAG, "onCreate");
        activities = new LinkedList<Activity>();

        mLocationClient = new LocationClient(this);

        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(false);
         /*
                            返回国测局经纬度坐标系 coor=gcj02
                            返回百度墨卡托坐标系 coor=bd09
                            返回百度经纬度坐标系 coor=bd09ll
         */
        option.setCoorType("gcj02");
        option.setScanSpan(GET_LOCATION_INTERVAL);
        option.disableCache(false);// 禁止启用缓存定位
        option.setProdName(getPackageName());
        option.setPriority(LocationClientOption.NetWorkFirst);
        option.setTimeOut(GET_LOCATION_TIME_OUT);

        // option.setAddrType("detail");
        // option.setPoiNumber(5); //最多返回POI个数
        // option.setPoiDistance(1000); //poi查询距离
        // option.setPoiExtraInfo(true); //是否需要POI的电话和地址等详细信息

        mLocationClient.setLocOption(option);
        mLocationClient.registerLocationListener(mLocListener);
        mLocationClient.start();
    }

    private class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation arg0) {
            int type = arg0.getLocType();
            switch (type) {
            case BDLocation.TypeCacheLocation:
                onGetMyLocationFinish(arg0.getLongitude(), arg0.getLatitude());
                break;
            case BDLocation.TypeGpsLocation:
                onGetMyLocationFinish(arg0.getLongitude(), arg0.getLatitude());
                break;
            case BDLocation.TypeNetWorkLocation:
                onGetMyLocationFinish(arg0.getLongitude(), arg0.getLatitude());
                break;
            default:
                LogUtil.w(TAG, "定位失败");
                if(null != mGetLocationListener){
                    String loc = GeePrefrence.getMyLocation(getApplicationContext());
                    String[] strs = loc.split(",");
                    mGetLocationListener.onFail(new double[]{Double.valueOf(strs[0]), Double.valueOf(strs[1])});
                    mGetLocationListener = null;
                }
                break;
            }
        }
        @Override
        public void onReceivePoi(BDLocation arg0) {}
    }

    private void onGetMyLocationFinish(double lon, double lat) {
        LogUtil.i(TAG, "定位成功, lon:" + lon + " lat:" + lat);
        GeePrefrence.setMyLocation(this, lon+","+lat);
        if(null != mGetLocationListener){
            mGetLocationListener.onFinish(new double[]{lon, lat});
            mGetLocationListener = null;
        }
    }

    public void addActivity(Activity param) {
        if (null != activities && null != param) {
            activities.add(param);
        }
    }

    public void clearActivities() {
        if (null != activities) {
            while (!activities.isEmpty()) {
                Activity a = activities.poll();
                a.finish();
            }
        }
    }

    @Override
    public void onTerminate() {
        LogUtil.v(TAG, "onTerminate");
        DataBaseHelper.close();
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        LogUtil.v(TAG, "onLowMemory");
    }

}
